<?php

class Performances_model extends CI_Model {

    function  __construct() {
        parent::__construct();
        $this->tableName = 'mis_performance';
        $this->load->model("functions_model");
        $this->load->model("consolidate_model");
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function newPerformance($added_by) {
        $data = $_POST;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function getOne($tid) {
        $data['tid'] = $tid;
        $this->db->select('tid,added_by,department,budget,quarter1,quarter2,quarter3,quarter4,date_added');
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);

            $list = $this->consolidate_model->getDepartments($row['department']);
            if (count($list) > 0) {
                $row['department'] = $list[0]->name;
            }
            $row['quarter1'] = $this->consolidate_model->getPercentage($row['quarter1'], $row['budget']);
            $row['quarter2'] = $this->consolidate_model->getPercentage($row['quarter2'], $row['budget']);
            $row['quarter3'] = $this->consolidate_model->getPercentage($row['quarter3'], $row['budget']);
            $row['quarter4'] = $this->consolidate_model->getPercentage($row['quarter4'], $row['budget']);
            $row['more2'] = "<a href='javascript:loadUrl(\"#performances_result\",\"" . base_url() . "index.php?/more_info/performancesMoreInfo/" . $row['tid'] . "\")'>More details</a>";
            unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

    function getAll($username, $tid=NULL) {
        if ($tid) {
            $data['tid'] = $tid;
        } else {
            $data['added_by'] = $username;
        }
        $this->db->select('date_added, tid,department,budget,quarter1,quarter2,quarter3,quarter4');
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
           // $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);

            $list = $this->consolidate_model->getDepartments($row['department']);
            if (count($list) > 0) {
                $row['department'] = $list[0]->name;
            }
            $row['quarter1'] = $this->consolidate_model->getPercentage($row['quarter1'], $row['budget']);
            $row['quarter2'] = $this->consolidate_model->getPercentage($row['quarter2'], $row['budget']);
            $row['quarter3'] = $this->consolidate_model->getPercentage($row['quarter3'], $row['budget']);
            $row['quarter4'] = $this->consolidate_model->getPercentage($row['quarter4'], $row['budget']);
            $row['more'] = "<a href='javascript:loadUrl(\"#performances_result\",\"" . base_url() . "index.php?/more_info/performancesMoreInfo/" . $row['tid'] . "\")'>More details</a>";
            unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

    function adminGetAll($field=NULL, $search=NULL) {
        if ($field and $search) {
            $data[$field] = $search;
            $this->db->like($data);
        }
        $this->db->select('date_added, tid,department,budget,quarter1,quarter2,quarter3,quarter4');
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
            $list = $this->consolidate_model->getDepartments($row['department']);
            if (count($list) > 0) {
                $row['department'] = $list[0]->name;
            }
            $row['quarter1'] = $this->consolidate_model->getPercentage($row['quarter1'], $row['budget']);
            $row['quarter2'] = $this->consolidate_model->getPercentage($row['quarter2'], $row['budget']);
            $row['quarter3'] = $this->consolidate_model->getPercentage($row['quarter3'], $row['budget']);
            $row['quarter4'] = $this->consolidate_model->getPercentage($row['quarter4'], $row['budget']);
            $row['more'] = "<a href='javascript:loadUrl(\"#performances_result\",\"" . base_url() . "index.php?/more_info/performancesMoreInfo/" . $row['tid'] . "\")'>More details</a>  ";

            if ($this->session->userdata("admin") != FALSE) {
                $row['more'].="  <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deletePerformance/$tid\",\"Are you sure to delete this record?\")'>Delete</a> ";
            }
            unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

}

?>
